﻿using System;
using System.IO;
using System.Linq;
using EnvDTE;
using System.Xml.Linq;

namespace EnvDTE_Sample {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("ActiveProject FullName: " + getActiveProjectPath());
            Console.WriteLine("removing lib folder from ActiveProject...");
            removeLibFolderFromActiveProjectPath();
            Console.WriteLine("lib folder removed");
            Console.ReadKey();
        }

        static string getActiveProjectPath() {
            string exMessage = string.Empty;
            try {
                DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
                Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
                if (activeSolutionProjects != null && activeSolutionProjects.Length > 0) {
                    Project activeProject = activeSolutionProjects.GetValue(0) as EnvDTE.Project;
                    return activeProject.FullName;
                }
            } catch (Exception ex) { exMessage = ex.Message; }

            throw new NullReferenceException("There is no ActiveSolutionProjects. " + exMessage);
        }

        static void removeLibFolderFromActiveProjectPath() {
            string exMessage = string.Empty;
            try {
                DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
                Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
                if (activeSolutionProjects != null && activeSolutionProjects.Length > 0) {
                    Project activeProject = activeSolutionProjects.GetValue(0) as EnvDTE.Project;
                    activeProject.ProjectItems.Item("lib").Remove();
                }
            } catch (Exception ex) { exMessage = ex.Message; }

            throw new NullReferenceException("Lib folder could not be removed. " + exMessage);
        }

        static string getActiveProjectDirectory() {
            string exMessage = string.Empty;
            try {
                DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
                Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
                if (activeSolutionProjects != null && activeSolutionProjects.Length > 0) {
                    Project activeProject = activeSolutionProjects.GetValue(0) as EnvDTE.Project;
                    return Path.GetDirectoryName(activeProject.FullName);
                }
            } catch (Exception ex) { exMessage = ex.Message; }

            throw new NullReferenceException("There is no ActiveSolutionProjects. " + exMessage);
        }

        /// <summary>
        /// TODO: refactor this, using Path.Combine
        /// </summary>
        static void test() {
            var filePath = Directory.GetFiles(getActiveProjectDirectory(), "*.csproj", SearchOption.AllDirectories).Single();

            if (filePath != null) {
                XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
                XDocument projDefinition = XDocument.Load(filePath);
                XElement project = projDefinition.Element(msbuild + "Project");


                XElement propertyGroup = project.Elements(msbuild + "PropertyGroup")
                    .Single(p => p.Attributes().Where(
                        att => att.Name == "Condition" &&
                        att.Value == " '$(Configuration)|$(Platform)' == 'Debug|x86' ")
                        .Count() > 0);

                string outputPath = propertyGroup.Elements(msbuild + "OutputPath").First().Value;

                //string outputPath = @"..\..\build\AgroBrasil.Cadastro.WindowsForms\bin\Debug\";
                outputPath = outputPath.Remove(0, 6);

                string activePath = getActiveProjectPath();

                int lastIndex;
                int toEnd;
                for (int i = 0; i < 2; i++) {
                    lastIndex = activePath.LastIndexOf("\\");
                    toEnd = activePath.Count();
                    activePath = activePath.Remove(lastIndex, toEnd - lastIndex);
                }

                activePath += "\\" + outputPath;
                //<OutputPath>..\..\build\AgroBrasil.Cadastro.WindowsForms\bin\Debug\</OutputPath>
            }
        }
    }
}
